package com.project.common.core.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.project.common.core.config.prop.PropAttributes;
import com.project.common.core.config.prop.PropConfig;
import com.project.common.core.utils.exception.CustomException;
import com.project.common.core.utils.exception.RESP_CODE_ENUM;
import com.project.common.core.utils.file.FilePathConsts;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.InputStream;
import java.util.Date;

/**
 * @ClassName OssUtils
 * @Description
 * @Author 王天豪
 * @Date 2020/7/29 12:53 AM
 * @Version 1.0
 */
public class OssUtils {

    public static Logger log = LoggerFactory.getLogger(OssUtils.class);

    /**
     * 上传文件至OSS文件服务器
     *
     * @param uploadFileStream
     * @param fileCode
     * @return
     */
    public static String upLoad(InputStream uploadFileStream, Integer fileCode,String fileName) {

        // 文件路径
        String filePath = FilePathConsts.SECOND_ENUM.getFilePath(fileCode);


        String ossPth = filePath + File.separator + DateUtil.convertDateToStr(new Date(), "yyyy-MM-dd") + File.separator + System.currentTimeMillis()+RandomStringUtils.randomNumeric(6)+"."+fileName.substring(fileName.lastIndexOf(".")+1);
        try {
            // 初始化上传参数
            // 内网【生产使用】
//            String endpoint = "oss-cn-beijing.aliyuncs.com";

            // 公网
            String endpoint = PropConfig.getProperty(PropAttributes.OSS_ENDPOINT);
            String accessKeyId = PropConfig.getProperty(PropAttributes.OSS_ACCESSKEYID);
            String accessKeySecret = PropConfig.getProperty(PropAttributes.OSS_ACCESSKEYSECRET);
            String bucketName = PropConfig.getProperty(PropAttributes.OSS_BUCKETNAME);
            ;

            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            ossClient.putObject(bucketName, ossPth, uploadFileStream);
            ossClient.shutdown();

        } catch (Exception e) {
            log.info("上传文件服务器出错");
            throw new CustomException(RESP_CODE_ENUM.MISS_PARAMETER);
        }

        return "/"+ossPth;
    }

    public static OSS getOssClient() {
        // 公网
        String endpoint = PropConfig.getProperty(PropAttributes.OSS_ENDPOINT);
        String accessKeyId = PropConfig.getProperty(PropAttributes.OSS_ACCESSKEYID);
        String accessKeySecret = PropConfig.getProperty(PropAttributes.OSS_ACCESSKEYSECRET);

        // 创建OSSClient实例。
        return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }

    /**
     * OSS文件删除
     * @param path
     */
    public static void delete(String path) {
        String endpoint = PropConfig.getProperty(PropAttributes.OSS_ENDPOINT);
        String accessKeyId = PropConfig.getProperty(PropAttributes.OSS_ACCESSKEYID);
        String accessKeySecret = PropConfig.getProperty(PropAttributes.OSS_ACCESSKEYSECRET);
        String bucketName = PropConfig.getProperty(PropAttributes.OSS_BUCKETNAME);

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 删除文件。如需删除文件夹，请将ObjectName设置为对应的文件夹名称。如果文件夹非空，则需要将文件夹下的所有object删除后才能删除该文件夹。
        ossClient.deleteObject(bucketName, path);

        // 关闭OSSClient。
        ossClient.shutdown();

    }
}
